package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuShop;
import com.hualan.dao.impl.MenDianDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/MenDian/query")
public class queryMenDianServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String strPageNum = request.getParameter("pageNum");
        String strPageSize = request.getParameter("pageSize");
        Integer pageNum = strPageNum.equals("") ? 1 : Integer.parseInt(strPageNum);
        Integer pageSize = strPageSize.equals("") ? 5 : Integer.parseInt(strPageSize);
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        MenDianDaoImpl menDianDao = new MenDianDaoImpl();
        List<PanghuShop> list = menDianDao.queryCondition(name, phone, startTime, endTime, pageNum, pageSize);
        //新建返回map集合
        HashMap<String, Object> map = new HashMap<>();
        if (list != null) {
            map.put("code", 200);
            map.put("message", "查询成功！");
        } else {
            map.put("code", 300);
            map.put("message", "查询失败！");
        }
        map.put("list", list);
        //将数据返回到前台
        response.getWriter().write(JSON.toJSONString(map));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
